<?php

class KitListModel
{
    private $diaryListCacheModel;
    
    function __construct()/*{{{*/
    {
        $this->diaryListCacheModel  =   new DiaryListCacheModel();
    }/*}}}*/
    
    public function addKitList($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KLM001';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $data   =   $this->getKitValue($kitInfo);
        if (empty($data)    &&  !empty(AC::$errorNo))   return  FALSE;

        $data['uid']    =   $kitInfo['uid'];
        $data['bodyId'] =   $kitInfo['bodyId'];

        isset($kitInfo['title'])            &&  $data['title']          =   $kitInfo['title'];
        isset($kitInfo['content'])          &&  $data['abbreviation']   =   UK::getstr($kitInfo['content'], 140, "...");
        isset($kitInfo['secret'])           &&  $data['secret']         =   $kitInfo['secret'];
        isset($kitInfo['attachment'][0])    &&  $data['attachmentPath'] =   $kitInfo['attachment'][0];

        $result =   Storage::addData($key, $data);

        if (empty($result))
        {
            AC::$errorNo    =   'KLM002';
            return  FALSE;
        }
        return  $result;
    }/*}}}*/

    public function getKitList($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']))
        {
            AC::$errorNo    =   'KLM003';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $start  =   isset($kitInfo['start'])  ?   $kitInfo['start'] :   0;
        $length =   isset($kitInfo['length']) ?   $kitInfo['length']:   AC::DIARY_LIST_DEFAULT_LEN;
        $orderBy=   ' markDate DESC ';

        $result =   Storage::getManyData($key, $orderBy, $start, $length);
        return  $result;
    }/*}}}*/

    public function getKitListInfo($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KLM001';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $where  =   array('bodyId' => $kitInfo['bodyId']);
        $result =   Storage::getConditionData($key, $where);
        return  $result;
    }/*}}}*/

    public function setKitList($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KLM001';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $data   =   $this->getKitValue($kitInfo);
        if (empty($data)    &&  !empty(AC::$errorNo))   return  FALSE;

        $data['uid']    =   $kitInfo['uid'];

        isset($kitInfo['title'])            &&  $data['title']          =   $kitInfo['title'];
        isset($kitInfo['content'])          &&  $data['abbreviation']   =   UK::getstr($kitInfo['content'], 140, "...");
        isset($kitInfo['secret'])           &&  $data['secret']         =   $kitInfo['secret'];
        isset($kitInfo['attachment'][0])    &&  $data['attachmentPath'] =   $kitInfo['attachment'][0];

        $where  =   array('bodyId' => $kitInfo['bodyId']);
        $result =   Storage::setConditionData($key, $data, $where);

        if (empty($result))
        {
            AC::$errorNo    =   'KLM006';
            return  FALSE;
        }
        return  $result;
    }/*}}}*/

    public function countTimes($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KLM004';
            return  FALSE;
        }

        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;
    
        $data   =   array();
        isset($kitInfo['viewTimes'])      &&  $data['viewTimes']      =   1;
        isset($kitInfo['commentTimes'])   &&  $data['commentTimes']   =   1;
        isset($kitInfo['likeTimes'])      &&  $data['likeTimes']      =   1;
        isset($kitInfo['shareTimes'])     &&  $data['shareTimes']     =   1;
        isset($kitInfo['repostTimes'])    &&  $data['repostTimes']    =   1;
        if (empty($data))
        {
            AC::$errorNo    =   'KLM005';
            return  FALSE;
        }
        $this->diaryListCacheModel->addDiaryNums($kitInfo);
        $where  =   array('bodyId' => $kitInfo['bodyId']);
        $result =   Storage::changeNumValueAdd($key, $data, $where);
        return  $result;
    }/*}}}*/

    public function removeKitList($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KLM007';
            return  FALSE;
        }

        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $data   =   array('status' => 1);
        $where  =   array('bodyId' => $kitInfo['bodyId']);
        $result =   Storage::setConditionData($key, $data, $where);
        if (empty($result))
        {
            AC::$errorNo    =   'KLM008';
        }
        return  $result;
    }/*}}}*/

    private function getKitKeyPrefix($kitType, $id)/*{{{*/
    {
        $kitTypeArray   =   array(
                                AC::FEED_KIT_ANNIVERSARY    =>  'KAL/',
                                AC::FEED_KIT_BMI            =>  'KBL/',
                                AC::FEED_KIT_MENSES         =>  'KML/',
                                AC::FEED_KIT_NOTE           =>  'KNL/',
                                AC::FEED_KIT_SCHEDULE       =>  'KSL/',
                                AC::FEED_KIT_TALLYBOOK      =>  'KTL/',
                            );

        if (empty($kitTypeArray[$kitType]))
        {
            AC::$errorNo    =   'KLM009';
            return  FALSE;
        }
        $key    =   $kitTypeArray[$kitType]. $id;
        return  $key;
    }/*}}}*/

    private function getKitValue($kitInfo)/*{{{*/
    {
        $necessaryField =   array(
                                'anniversary'   =>  array(),
                                'bmi'           =>  array('height' => 1, 'weight' => 1,),
                                'menses'        =>  array('lastDay' => 1, 'during' => 1, 'average' => 1,),
                                'note'          =>  array(),
                                'schedule'      =>  array('complete' => 1,),
                                'tallybook'     =>  array('price' => 1, 'number' =>1, 'tallyType' => 1,),
                            );
        $inessentalField=   array(
                                'anniversary'   =>  array('markDate' => 1,),
                                'bmi'           =>  array(),
                                'menses'        =>  array(),
                                'note'          =>  array('markDate' => 1,),
                                'schedule'      =>  array('markDate' => 1,),
                                'tallybook'     =>  array('markDate' => 1,),
                            );
        $data   =   array();
        if (isset($necessaryField[$kitInfo['kitType']]) &&  !empty($necessaryField[$kitInfo['kitType']]))
        {
            foreach ($necessaryField[$kitInfo['kitType']]   as  $field  =>  $value)
            {
                if (empty($field))  continue;
                if (isset($kitInfo[$field]))
                {
                    $data[$field]   =   $kitInfo[$field];
                }
                else
                {
                    AC::$errorNo    =   'KLM010';
                    return  FALSE;
                }
            }
        }

        if (isset($inessentalField[$kitInfo['kitType']])&&  !empty($inessentalField[$kitInfo['kitType']]))
        {
            foreach ($inessentalField[$kitInfo['kitType']]  as  $field  =>  $value)
            {
                if (empty($field))  continue;
                if (isset($kitInfo[$field]))
                {
                    $data[$field]   =   $kitInfo[$field];
                }
            }
        }
        return  $data;
    }/*}}}*/

}


